package com.example.demo.controller;

import com.example.demo.domain.AgeCount;
import com.example.demo.domain.Employee;
import com.example.demo.result.RespBean;
import com.example.demo.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping(value = "/EmployeeManage")
@CrossOrigin
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;

    @GetMapping(value = "/findAll")
    @ResponseBody
    public RespBean findAll(){
        List<Employee> employees;
        employees = employeeService.findAll();
        return RespBean.success("search success",employees);
    }
    @GetMapping(value = "/findOneEmployee")
    @ResponseBody
    public RespBean findOneEmployee(@RequestParam("employee_id") int id){
        Employee employee;
        employee = employeeService.selectEmployee(id);
        return RespBean.success("search success",employee);
    }
    @PostMapping(value = "/addEmployee")
    @ResponseBody
    public RespBean addEmployee(@RequestBody Employee employee){
        Timestamp d = new Timestamp(System.currentTimeMillis());
        employee.setCreated(d.toString());
        employee.setImgset_dir("/img/employee");
        employee.setProfile_photo("/employee/profile/");
        employeeService.addEmployee(employee);
        return RespBean.success("add success");
    }
    @GetMapping(value = "/deleteEmployee")
    @ResponseBody
    public RespBean deleteEmployee(@RequestParam("employee_id") int id){
        employeeService.deleteEmployee(id);
        return RespBean.success("delete success");
    }
    @PostMapping(value = "updateEmployee")
    @ResponseBody
    public RespBean updateEmployee(@RequestBody Employee employee){
        employeeService.updateEmployee(employee);
        return RespBean.success("update success");
    }

    @GetMapping(value = "/selectByAge")
    @ResponseBody
    public RespBean SelectAge(){
        AgeCount ageCount = new AgeCount();
        int[] age = new int[4];
        age[0] = employeeService.selectAge(1,59);
        age[1] = employeeService.selectAge(60,69);
        age[2] = employeeService.selectAge(70,79);
        age[3] = employeeService.selectAge(80,150);
        ageCount.setAge(age);
        return RespBean.success("success",ageCount);
    }
}
